package CompletableFuture.Future;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @program: algorithm
 * @description: 缺点 1.get阻塞 2.idDone轮询
 * @author: 陈恩涛
 * @create: 2022-08-08 22:38
 **/
public class weakness {

    public static void main(String[] args) {
        final FutureTask<String> futureTask = new FutureTask<String>(() -> {
            System.out.println(Thread.currentThread().getName() + "\t --------come in");
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "task over";
        });

        final Thread t1 = new Thread(futureTask, "t1");
        t1.start();

        System.out.println(Thread.currentThread().getName() + "\t --------忙其他任务去了");

        while (true) {
            if (futureTask.isDone()) {
                try {
                    System.out.println(futureTask.get());
                } catch (InterruptedException | ExecutionException  e) {
                    e.printStackTrace();
                }
                break;
            } else {
                try {
                    TimeUnit.MILLISECONDS.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("正在处理中……");
            }
        }
    }
}
